package cn.dxh.stagefinal.utils;

import cn.dxh.stagefinal.bean.Employee;
import cn.dxh.stagefinal.bean.Order;
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;

import java.io.IOException;
import java.io.OutputStream;
import java.util.List;

/**
 * IntelliJ IDEA -> stagefinal
 * 2019/12/12 15:10
 *
 * @author zmyao
 * 别看，我是渣渣。
 */
//导出数据
public class OrderExcelUtils {
    //1.先创建一个Excel文件
    static HSSFWorkbook workbook;
    //2.创建文件中的sheet表   参数是sheet 名称
    static HSSFSheet sheet;
    //3.创建sheet中的行列信息
    //3.1创建第一行
    public static void  createFirst(String headname){
        workbook = new HSSFWorkbook();
         sheet = workbook.createSheet(headname);
        HSSFRow row = sheet.createRow(0);
        String[] heads ={"订单编号","实付金额","下单时间","订单来源","订单形式","订单状态","配送员","商品id","客户id","商品数量","支付时间","商品名称","客户名称","订单金额","优惠金额","优惠方式","支付方式","订单编号"};
        for (int i = 0; i < heads.length; i++) {
            HSSFCell cell = row.createCell(i);
            cell.setCellValue(heads[i]);
        }
    }

    //3.2创建除第一行以外的其他行，参数表示的被导出的数据的集合
    public static void createOthers(List<Order>  list){
        for (int i = 0; i < list.size(); i++) {
            HSSFRow row = sheet.createRow(i + 1);
            Order order = list.get(i);
            HSSFCell cella = row.createCell(0);
            cella.setCellValue(order.getOrderid());

            HSSFCell cellb = row.createCell(1);
            cellb.setCellValue(order.getAmountpaid().doubleValue());

            HSSFCell cellc = row.createCell(2);
            cellc.setCellValue(order.getOrderdate());

            HSSFCell celld = row.createCell(3);
            celld.setCellValue(order.getSource());

            HSSFCell celle= row.createCell(4);
            celle.setCellValue(order.getShape());

            HSSFCell cellg= row.createCell(5);
            if ( order.getOrderstate() == 1) {
                cellg.setCellValue("未支付");
            }else if (order.getOrderstate() == 2){
                cellg.setCellValue("待发货");
            }else if (order.getOrderstate() == 3){
                cellg.setCellValue("已发货");
            }else if (order.getOrderstate() == 4){
                cellg.setCellValue("已完成");
            }
            else  {
                cellg.setCellValue("已取消");
            }
//            "配送员","商品id","客户id","商品数量","支付时间","商品名称","客户名称","订单金额","优惠金额","优惠方式","支付方式","订单编号"
            HSSFCell cellf= row.createCell(6);
            cellf.setCellValue(order.getDeliveryman());

            HSSFCell celli= row.createCell(7);
            celli.setCellValue(order.getCommid());

            HSSFCell cellh= row.createCell(8);
            cellh.setCellValue(order.getClientid());

            HSSFCell celll= row.createCell(9);
            celll.setCellValue(order.getNum());

            HSSFCell cellm= row.createCell(10);
            cellm.setCellValue(order.getPaytime());

            HSSFCell celln= row.createCell(11);
            celln.setCellValue(order.getCommodity().getCommName());

            HSSFCell cello= row.createCell(12);
            cello.setCellValue(order.getClient().getUsername());
//            "订单金额","优惠金额","优惠方式","支付方式","订单编号"
            HSSFCell cellp= row.createCell(13);
            cellp.setCellValue(order.getOrdermoney().doubleValue());

            HSSFCell cellq= row.createCell(14);
            cellq.setCellValue(order.getYhmoney().doubleValue());

            HSSFCell cellr= row.createCell(15);
            cellr.setCellValue(order.getYhway());

            HSSFCell cells= row.createCell(16);
            cells.setCellValue(order.getPayway());

            HSSFCell cellt= row.createCell(16);
            cellt.setCellValue(order.getOrdernum());
        }
    }
//4.设置导出信息
    public static void export(OutputStream outputStream) throws IOException {
        //设置输出的方式是以网格的方式输出
        sheet.setGridsPrinted(true);
        //将文件写入到输出流中
        workbook.write(outputStream);
        outputStream.flush();
        outputStream.close();
    }

}
